package com.pyb.redis.mapper;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/3/18 15:37
 * @注释
 */
import com.pyb.redis.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.Map;

@Repository
public class StudentRepository {

    private static final String KEY = "Student";

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Resource(name="redisTemplate")
    private HashOperations<String, String, Student> hashOperations;

    public void saveStudent(Student student) {
        hashOperations.put(KEY, student.getId(), student);
    }

    public Map<String, Student> findAllStudents() {
        return hashOperations.entries(KEY);
    }

    public Student findStudentById(String id) {
        return hashOperations.get(KEY, id);
    }

    public void updateStudent(Student student) {
        saveStudent(student);
    }

    public void deleteStudent(String id) {
        hashOperations.delete(KEY, id);
    }
}
